// pages/fuli/fuli.js
var app = getApp();
var Crypto = require('../../utils/cryptojs/cryptojs.js').Crypto;
Page({

  /**
   * 页面的初始数据 
   */
  data: {
    advert:[],
    userStyle:1,
    advertCount: "0",
    balance:"0",
    sendMoneys: "0",
    winMoneys: "0",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getUserInfo({
      success:function(res){
        that.setData({
          userInfo: res.userInfo,
        })
      }
    })
   

  },
  ChangeTab:function(e){
    var that= this;
    that.setData({
      userStyle:e.currentTarget.dataset.id,
    })
  },
  // 请求商家发红包信息
  advertInfo:function(){
    var that = this;
    wx.getStorage({
      key: 'token',
      success: function(res) {
        var token = res.data;
        var timestamp = app.timestamp();
        var url = app.globalData.localApi+"/advert/info";
        wx.request({
          url: url,
          method:"POST",
          data:{
            timestamp:timestamp,
            token:token,
            sign:Crypto.MD5(app.sign(url,token,timestamp)),
            url:app.urlStr(url)
          },
          success:function(res){
            console.log(res)
            if(res.data.code==0){
              that.setData({
                advertCount:res.data.data.advertCount,
                balance:res.data.data.balance,
                sendMoneys: res.data.data.sendMoneys,
                winMoneys: res.data.data.winMoneys,
              })
            }
          },
          fail:function(res){
            console.log(res)
          }
        })
      },
    })
  },
// 请求消费者信息
  shopList:function(){
    var that = this;
    wx.getStorage({
      key: 'token',
      success: function(res) {
        var token = res.data;
        var url = app.globalData.localApi +"/advert/shopList";
        var timestamp = app.timestamp();
        wx.request({
          url: url,
          method:"POST",
          data:{
            token:token,
            url:app.urlStr(url),
            timestamp:timestamp,
            sign:Crypto.MD5(app.sign(url,token,timestamp))
          },
          success:function(res){
            console.log(res)
            if(res.data.code==0){
              that.setData({
                advert:res.data.data
              })
            }
          }
        })
      },
    })
  },
  toQuestion:function(){
    var that = this;
    wx.navigateTo({
      url: '../question/question',
    })
  },
  toTixian:function(){
    wx.navigateTo({
      url: '../tixian/tixian',
    })
  },
  tomyRecord:function(){
    wx.navigateTo({
      url: '../index/mRecord/myRecord',
    })
  },
  toadvert:function(){
    var that = this;
    if (that.data.advertCount == 0 || that.data.advertCount==''){
      app.showInfo("您还没有发布广告。。")
    }else{
      wx.navigateTo({
        url: './advert/advert',
      })
    }

  },
  toshopShare:function(e){
    var that = this;
    var orderno = e.currentTarget.dataset.orderno;
    console.log("orderno :" + orderno)
    wx.navigateTo({
      url: './shopShare/shopShare?style=1&orderno='+orderno,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.shopList();
    that.advertInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})